"""
基础模型类
"""
from sqlalchemy import Column, Integer, DateTime, Boolean, String, Text
from sqlalchemy.sql import func
from app.core.database import Base

class BaseModel(Base):
    """所有模型的基础类"""
    __abstract__ = True
    
    # 基础字段（所有表通用）
    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID')
    is_active = Column(Boolean, default=True, nullable=False, comment='是否活跃')
    created_at = Column(DateTime, default=func.now(), nullable=False, comment='创建时间')
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), nullable=False, comment='更新时间')
    created_by = Column(String(50), default='system', nullable=False, comment='创建人')
    updated_by = Column(String(50), default='system', nullable=False, comment='更新人')
    version = Column(Integer, default=1, nullable=False, comment='版本号')
    is_deleted = Column(Boolean, default=False, nullable=False, comment='是否删除')
    
    def to_dict(self):
        """转换为字典"""
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
    
    def __repr__(self):
        return f"<{self.__class__.__name__}(id={self.id})>"